package cn.zjyj.module.zjyj.controller.admin.deviceinstanceacceptancerecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.zjyj.framework.common.common.pojo.PageParam;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.pojo.CommonResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;
import static cn.zjyj.framework.common.common.pojo.CommonResult.success;

import cn.zjyj.framework.excel.core.util.ExcelUtils;

import cn.zjyj.framework.apilog.core.annotation.ApiAccessLog;
import static cn.zjyj.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.zjyj.module.zjyj.controller.admin.deviceinstanceacceptancerecord.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.deviceinstanceacceptancerecord.DeviceInstanceAcceptanceRecordDO;
import cn.zjyj.module.zjyj.service.deviceinstanceacceptancerecord.DeviceInstanceAcceptanceRecordService;

@Tag(name = "管理后台 - 设备实例验收单")
@RestController
@RequestMapping("/zjyj/device-instance-acceptance-record")
@Validated
public class DeviceInstanceAcceptanceRecordController {

    @Resource
    private DeviceInstanceAcceptanceRecordService deviceInstanceAcceptanceRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建设备实例验收单")
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance-acceptance-record:create')")
    public CommonResult<Long> createDeviceInstanceAcceptanceRecord(@Valid @RequestBody DeviceInstanceAcceptanceRecordSaveReqVO createReqVO) {
        return success(deviceInstanceAcceptanceRecordService.createDeviceInstanceAcceptanceRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备实例验收单")
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance-acceptance-record:update')")
    public CommonResult<Boolean> updateDeviceInstanceAcceptanceRecord(@Valid @RequestBody DeviceInstanceAcceptanceRecordSaveReqVO updateReqVO) {
        deviceInstanceAcceptanceRecordService.updateDeviceInstanceAcceptanceRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备实例验收单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance-acceptance-record:delete')")
    public CommonResult<Boolean> deleteDeviceInstanceAcceptanceRecord(@RequestParam("id") Long id) {
        deviceInstanceAcceptanceRecordService.deleteDeviceInstanceAcceptanceRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备实例验收单")
                @PreAuthorize("@ss.hasPermission('zjyj:device-instance-acceptance-record:delete')")
    public CommonResult<Boolean> deleteDeviceInstanceAcceptanceRecordList(@RequestParam("ids") List<Long> ids) {
        deviceInstanceAcceptanceRecordService.deleteDeviceInstanceAcceptanceRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备实例验收单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance-acceptance-record:query')")
    public CommonResult<DeviceInstanceAcceptanceRecordRespVO> getDeviceInstanceAcceptanceRecord(@RequestParam("id") Long id) {
        DeviceInstanceAcceptanceRecordDO deviceInstanceAcceptanceRecord = deviceInstanceAcceptanceRecordService.getDeviceInstanceAcceptanceRecord(id);
        return success(BeanUtils.toBean(deviceInstanceAcceptanceRecord, DeviceInstanceAcceptanceRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备实例验收单分页")
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance-acceptance-record:query')")
    public CommonResult<PageResult<DeviceInstanceAcceptanceRecordRespVO>> getDeviceInstanceAcceptanceRecordPage(@Valid DeviceInstanceAcceptanceRecordPageReqVO pageReqVO) {
        PageResult<DeviceInstanceAcceptanceRecordDO> pageResult = deviceInstanceAcceptanceRecordService.getDeviceInstanceAcceptanceRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceInstanceAcceptanceRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备实例验收单 Excel")
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance-acceptance-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceInstanceAcceptanceRecordExcel(@Valid DeviceInstanceAcceptanceRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceInstanceAcceptanceRecordDO> list = deviceInstanceAcceptanceRecordService.getDeviceInstanceAcceptanceRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备实例验收单.xls", "数据", DeviceInstanceAcceptanceRecordRespVO.class,
                        BeanUtils.toBean(list, DeviceInstanceAcceptanceRecordRespVO.class));
    }

}